package com.junwell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junwell.common.dto.PageDto;
import com.junwell.common.lang.Result;
import com.junwell.entity.Standard;
import com.junwell.entity.Station;
import com.junwell.mapper.StationMapper;
import com.junwell.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/standard")
public class StandardController {

    @Autowired
    private StandardService standardService;

    @Autowired
    private StationMapper stationMapper;

    @GetMapping("findPage")
    public IPage<Standard> findPage(PageDto pageDto){
        IPage<Standard> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<Standard> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("rule"  , pageDto.getQueryString());
        page  = standardService.page(page, queryWrapper);
        return page;
    }

    @PostMapping("add")
    public Result add(@RequestBody Standard standard){
        boolean add = standardService.add(standard);
        if (add){
            return Result.succ("成功");
        }else {
            return Result.fail("添加失败");
        }
    }



    @GetMapping("{id}")
    public Result viewOne(@PathVariable("id")Integer id){
        Standard standard = standardService.viewOne(id);
        if (standard !=null){
            return Result.succ(200,"成功",standard);
        }else {
            return Result.fail("失败");
        }
    }


    @PostMapping("update")
    public Result update(@RequestBody Standard standard){
        boolean update =standardService.update(standard);
        if (update){
            return Result.succ("成功");
        }else {
            return Result.fail("失败");
        }
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Integer id ){
        boolean delete = standardService.delete(id);
        if (delete){
            return Result.succ("成功");
        }else {
            return Result.fail("失败");
        }
    }

    @GetMapping("findstation")
    public Result findUser(){
        QueryWrapper<Station> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("station_name");
        List<Station> stations = stationMapper.selectList(queryWrapper);
        return  Result.succ(stations);
    }
}
